package com.woniuxy.purchasemanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import com.woniuxy.commons.entity.purchaseManager.PurchaseOrderEntity;
import com.woniuxy.commons.entity.purchaseManager.PurchaseOrderGoodsEntity;
import com.woniuxy.commons.entity.purchaseManager.base.PageResult;
import com.woniuxy.commons.entity.purchaseManager.base.QueryPageBean;
import com.woniuxy.commons.entity.purchaseManager.dto.PurchaseOrderGoodsDTO;
import com.woniuxy.commons.utils.PageUtils;
import com.woniuxy.commons.utils.Query;
import com.woniuxy.purchasemanager.dao.PurchaseOrderDao;
import com.woniuxy.purchasemanager.dao.PurchaseOrderGoodsDao;
import com.woniuxy.purchasemanager.service.PurchaseOrderGoodsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

@Service("purchaseOrderGoodsService")
public class PurchaseOrderGoodsServiceImpl extends ServiceImpl<PurchaseOrderGoodsDao, PurchaseOrderGoodsEntity> implements PurchaseOrderGoodsService {

    @Resource
    private PurchaseOrderGoodsDao purchaseOrderGoodsDao;

    @Resource
    private PurchaseOrderDao purchaseOrderDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseOrderGoodsEntity> page = this.page(
                new Query<PurchaseOrderGoodsEntity>().getPage(params),
                new QueryWrapper<PurchaseOrderGoodsEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageResult<PurchaseOrderGoodsDTO> findPage(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();

        Integer pageSize = queryPageBean.getPageSize();
        //查询条件
        String queryString = queryPageBean.getQueryString();
        //分页查询
        PageMethod.startPage(currentPage, pageSize);

        Page<PurchaseOrderGoodsDTO> page = purchaseOrderGoodsDao.findPageByCondition(queryString);

        return new PageResult<>(page.getTotal(), page.getResult());
    }

    //采购单编辑提交后，将采购单状态设置为2表示为采购中------>在审核同意之后在设置为3表示审核通过已完成。
    @Transactional
    @Override
    public boolean updateByEdit(Integer x1, Integer x2, Integer purchaseOrderId) {
        int num = purchaseOrderDao.update(null,
                new UpdateWrapper<PurchaseOrderEntity>()
                        .eq("purchase_order_id", purchaseOrderId)
                        .set("product_line", x1)
                        .set("warehouse", x2)
                        .set("purchase_order_status",2));
        return num > 0;
    }


}